package com.icebergteam.warfacewidget.rank.ui;

import java.util.ArrayList;
import org.jsoup.Jsoup;
import com.icebergteam.warfacewidget.AppManager;
import com.icebergteam.warfacewidget.core.ListViewFactory;
import com.icebergteam.warfacewidget.core.LogManager;
import com.icebergteam.warfacewidget.core.utils.ViewUtils;
import com.icebergteam.warfacewidget.data.sql.RankTable;
import com.icebergteam.warfacewidget.rank.ClassHelper;
import com.icebergteam.warfacewidget.rank.RankHelper;
import com.icebergteam.warfacewidget.rank.data.Top100Item;
import com.icebergteam.warfacewidget.rank.data.Top100Parser;
import com.icebergteam.warfacewidget.R;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.text.Html;
import android.view.View;
import android.widget.RemoteViews;

public class Top100ListViewFactory extends ListViewFactory  {
	public static String className = Top100ListViewFactory.class
			.getCanonicalName();
	
	private ArrayList<Top100Item> listTop100Items = new ArrayList<Top100Item>();

	public Top100ListViewFactory(Context ctxt, Intent intent) {
		super(ctxt, intent);
		try {
			Top100Parser parser = new Top100Parser();
			
			//FIXME: url - id for request top100
			Cursor cursor = AppManager.getDatabase().getRankTable().getRankByUrl("http://wf.mail.ru/dynamic/ajax/index.php?do=get_ladder_info&server=0&category=1&subcategory=0");
				
			while(cursor!= null && cursor.moveToNext()){
				listTop100Items = parser.parseXML(Jsoup.parse(cursor.getString(cursor.getColumnIndex(RankTable.FIELD_HTML.trim()))), AppManager.getTop100Settings().getResultItemsCount());
				
			}
			
			count = listTop100Items.size();
			
			if(listTop100Items.size() > AppManager.getTop100Settings().getResultItemsCount()){
				count = AppManager.getTop100Settings().getResultItemsCount();
			}
			
			cursor.close();
			LogManager.LogV(Top100ListViewFactory.className,LogManager.getExecuteMethodName(),
							((cursor == null) ? cursor == null
									: cursor.toString()),
							((ctxt == null) ? ctxt == null : ctxt.toString()), listTop100Items, count);
		
		} catch (Exception e) {
			LogManager.LogEA(className, "bug", ViewUtils.getErrorMessage(e));
		} 
	}

	@Override
	public RemoteViews getViewAt(int position) {
		try {
			RemoteViews row = new RemoteViews(context.getPackageName(),
					R.layout.top100_list_item_widget_layout);
			
			LogManager.LogV(className, LogManager.getExecuteMethodName(), position, count);
			
			if(listTop100Items != null && !listTop100Items.isEmpty() && listTop100Items.get(position) != null){
				int rank = listTop100Items.get(position).getPos();
				row.setImageViewResource(R.id.widget_rank_top100_list_item_rank_image, RankHelper.getImageIdForRank(rank));
				
				if(rank > 3) {
					row.setTextViewText(R.id.widget_rank_top100_list_item_pos,
							String.format("%s", rank));
					row.setViewVisibility(R.id.widget_rank_top100_list_item_pos, View.VISIBLE);
				} else {
					row.setViewVisibility(R.id.widget_rank_top100_list_item_pos, View.GONE);
				}
				
				row.setTextViewText(R.id.widget_rank_top100_list_item_nickname,
						Html.fromHtml(listTop100Items.get(position).getNickname()));
					
									
				row.setImageViewResource(R.id.widget_rank_top100_list_item_class, 
						ClassHelper.getImageIdForClass(listTop100Items.get(position).getClasstime()));
				
				row.setProgressBar(R.id.widget_rank_top100_list_item_pos_progress, 100, listTop100Items.get(position).getProgress(), false);
//				Intent i = new Intent();
//				
//				i.putExtra(Top100ListViewFactory.POS_INTENT_KEY, (position));
//				row.setOnClickFillInIntent(R.id.LinearLayout1, i);
			
			
			}
			return row;
		} catch (NumberFormatException e) {
			LogManager.LogEA(className, "bug", ViewUtils.getErrorMessage(e));
		} catch (Exception e) {
			LogManager.LogEA(className, "bug", ViewUtils.getErrorMessage(e));
		}
		return null;
	}

	
}